<?php
function list_doc_in($depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_doc_in where edoc_id not in (select edoc_id from view_run_recieve where depart_id_run=$depart_id and depart_id_recieve=$depart_id) and depart_id_run=$depart_id order by depart_owner_id, doc_date;");
    else
        $ans = getPG("select * from view_run_doc_in where (secret<=$secret_u or depart_id_run=".$_SESSION['edoc_depart_id'].") and edoc_id not in (select edoc_id from view_run_recieve where depart_id_run=$depart_id and depart_id_recieve=$depart_id) and depart_id_run=$depart_id order by depart_owner_id, doc_date;");
	if(!isset($ans[0])){
        echo "<tr><td colspan=\"13\">";
		echo "<span class=\"err\">ไม่มีรายการ</span>";
        echo "</td></tr>";
		return;
	}

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_owner_id'];
	foreach($ans as $dat){
		if($dat['depart_owner_id']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "<tr><td colspan=\"13\">";
			echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
            echo "</td></tr>";
			$depart = $dat['depart_owner_id'];
			createDocList($dat2,0,$depart_id);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "<tr><td colspan=\"13\">";
	echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
    echo "</td></tr>";
	createDocList($dat2,0,$depart_id);
}

function list_doc_recieve($depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_recieve where depart_id_run=$depart_id and depart_id_recieve=$depart_id order by depart_owner_id,recieve_date desc,recieve_time desc;");
    else
        $ans = getPG("select * from view_run_recieve where (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") and depart_id_run=$depart_id and depart_id_recieve=$depart_id order by depart_owner_id,recieve_date desc,recieve_time desc;");
	if(!isset($ans[0])){
        echo "<tr><td colspan=\"13\">";
		echo "<span class=\"err\">ไม่มีรายการ</span>";
        echo "</td></tr>";
		return;
	}

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_owner_id'];
	foreach($ans as $dat){
		if($dat['depart_owner_id']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "<tr><td colspan=\"13\">";
			echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
            echo "</td></tr>";
			$depart = $dat['depart_owner_id'];
			createDocList($dat2,1,$depart_id);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "<tr><td colspan=\"13\">";
	echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
    echo "</td></tr>";
	createDocList($dat2,1,$depart_id);

}

function list_doc_send($depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_recieve where depart_id_run!=$depart_id and depart_id_recieve=$depart_id order by depart_id_run,recieve_date desc,recieve_time desc;");
    else
        $ans = getPG("select * from view_run_recieve where (secret<=$secret_u or depart_id_recieve=".$_SESSION['edoc_depart_id'].") and depart_id_run!=$depart_id and depart_id_recieve=$depart_id order by depart_id_run,recieve_date desc,recieve_time desc;");
	if(!isset($ans[0])){
        echo "<tr><td colspan=\"13\">";
		echo "<span class=\"err\">ไม่มีรายการ</span>";
        echo "</td></tr>";
		return;
	}

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_run'];
	foreach($ans as $dat){
		if($dat['depart_id_run']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "<tr><td colspan=\"13\">";
			echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
            echo "</td></tr>";
			$depart = $dat['depart_id_run'];
			createDocList($dat2,1,$depart_id);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "<tr><td colspan=\"13\">";
	echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
    echo "</td></tr>";
	createDocList($dat2,1,$depart_id);
}

function list_doc_send_to($depart_id){
	$secret_u = $_SESSION['edoc_secret'];
	if($depart_id==$_SESSION['edoc_depart_id'])
        $ans = getPG("select * from view_run_sent where depart_id_run!=$depart_id and depart_id_sent=$depart_id order by depart_id_run,sent_date desc,sent_time desc;");
    else
        $ans = getPG("select * from view_run_sent where (secret<=$secret_u or depart_id_sent=".$_SESSION['edoc_depart_id'].") and depart_id_run!=$depart_id and depart_id_sent=$depart_id order by depart_id_run,sent_date desc,sent_time desc;");
	if(!isset($ans[0])){
        echo "<tr><td colspan=\"13\">";
		echo "<span class=\"err\">ไม่มีรายการ</span>";
        echo "</td></tr>";
		return;
	}

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_run'];
	foreach($ans as $dat){
		if($dat['depart_id_run']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
            echo "<tr><td colspan=\"13\">";
			echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
            echo "</td></tr>";
			$depart = $dat['depart_id_run'];
			createDocList2($dat2,$depart_id);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
    echo "<tr><td colspan=\"13\">";
	echo "<span class=\"topic\">".get_depart_name($depart)."</span>";
    echo "</td></tr>";
	createDocList2($dat2,$depart_id);
}

function createDocList($l,$logic,$depart_id){
?>

<tr>
<?php if($logic==1){ ?>
<td>เลขที่รับ</td>
<?php }else{ ?>
<td> --- </td>
<?php } ?>
<td>เลขที่หนังสือ</td>
<td>วันที่หนังสือ</td>
<td>ชั้นความลับ</td>
<td>ความเร่งด่วน</td>
<td>จากหน่วยงาน</td>
<td>เรื่อง</td>
<td>ผู้ส่ง</td>
<?php
    if($logic==1){
?>
<td>แก้ไข</td>
<?php
    }else{
?>
<td>ลงรับ</td>
<?php
    }
?>
<td>เกษียน</td>
<td>PDF</td>
<?php if($logic==1){ ?>
<td>การปฏิบัติ</td>
<?php }else{ ?>
<td> --- </td>
<?php } ?>
<td>ลบ</td>
</tr>
<?php
	foreach($l as $dat){
		if($logic==1)
		{
        	if($dat['depart_owner_id']==$_SESSION['edoc_depart_id'])
            	echo "<tr><td>*".$dat['recieve_no']."</td>";
        	else
            	echo "<tr><td>".$dat['recieve_no']."</td>";
		}else echo "<tr><td> --- </td>";
		echo "<td>".$dat['doc_no']."</td><td>".dateThDeco($dat['doc_date'])."</td>";
		echo "<td>".shSecret($dat['secret'])."</td><td>".shRapid($dat['rapid'])."</td>";
		echo "<td>".shDepartSendFast($dat['depart_send_id'],$dat['depart_name_send'],$dat['depart_send_name_out'])."</td>";
		echo "<td>".$dat['headline']."</td><td>".nl2br(htmlspecialchars($dat['sender']))."</td>";
        if($logic){
            if(canEditDoc($dat,$depart_id) || ($logic && ($depart_id==$_SESSION['edoc_depart_id']))){
                echo "<td align=\"center\"><a href=\"edit_doc.php?edoc_id=".$dat['edoc_id']."&depart_id=".$depart_id."\" ><img src=\"pic/edit.png\" /></a></td>";
            }else{
                echo "<td> </td>";
            }
        }else{
            if(canRecieveDoc($depart_id,$logic)){
                echo "<td align=\"center\"><a href=\"recieve.php?edoc_id=".$dat['edoc_id']."&depart_id=$depart_id\"><img src=\"pic/r_arrow.gif\" /></a></td>";
            }else{
                echo "<td> </td>";
            }
        }
        if(canReadKseanDoc($depart_id)){
            //echo "<td align=\"center\">".isKseanAlready($dat['edoc_id'],$depart_id)."<a href=\"ksean.php?edoc_id=".$dat['edoc_id']."&depart_id=$depart_id\"><img src=\"pic/ksean.jpg\" /></a></td>";
			echo "<td align=\"center\"><a href=\"ksean.php?edoc_id=".$dat['edoc_id']."&depart_id=$depart_id\"><img src=\"pic/ksean.jpg\" /></a></td>";
        }else{
            echo "<td> </td>";
        }
        if(canReadDoc($dat,$depart_id)){
            echo "<td align=\"center\"><a href=\"loadpdf.php?edoc_id=".$dat['edoc_id']."\" target=\"_blank\" ><img src=\"pic/pdfsmall.jpg\" /></a></td>";
        }else{
            echo "<td> </td>";
        }
		if($logic==1) echo "<td>".nl2br(htmlspecialchars($dat['comment']))."</td>";
		else echo "<td> --- </td>";
        if(canEditDoc($dat,$depart_id)){
            echo "<td align=\"center\"><img src=\"pic/drop.png\" /></td>";
        }else{
            echo "<td> </td>";
        }
		echo "</tr>";
	}
}

function createDocList2($l,$depart_id){
?>

<tr>
<td>เลขที่หนังสือ</td>
<td>วันที่หนังสือ</td>
<td>ชั้นความลับ</td>
<td>ความเร่งด่วน</td>
<td>เรียน</td>
<td>เรื่อง</td>
<td>ผู้ส่ง</td>
<td>แก้ไข</td>
<td>เกษียน</td>
<td>PDF</td>
<td>การปฏิบัติ</td>
<td>ลบ</td>
</tr>
<?php
	foreach($l as $dat){
		echo "<td>".$dat['doc_no']."</td><td>".dateThDeco($dat['doc_date'])."</td>";
		echo "<td>".shSecret($dat['secret'])."</td><td>".shRapid($dat['rapid'])."</td>";
		echo "<td>".$dat['reciever']."</td>";
		echo "<td>".$dat['headline']."</td><td>".nl2br(htmlspecialchars($dat['sender']))."</td>";
        if(canEditDoc($dat,$depart_id) || ($logic && ($depart_id==$_SESSION['edoc_depart_id']))){
	    	echo "<td align=\"center\"><a href=\"edit_doc_sent.php?edoc_id=".$dat['edoc_id']."&depart_id=".$depart_id."\" ><img src=\"pic/edit.png\" /></a></td>";
        }else{
        	echo "<td> </td>";
        }
        if(canReadKseanDoc($depart_id)){
            //echo "<td align=\"center\">".isKseanAlready($dat['edoc_id'],$depart_id)."<a href=\"ksean.php?edoc_id=".$dat['edoc_id']."&depart_id=$depart_id\"><img src=\"pic/ksean.jpg\" /></a></td>";
			echo "<td align=\"center\"><a href=\"ksean.php?edoc_id=".$dat['edoc_id']."&depart_id=$depart_id\"><img src=\"pic/ksean.jpg\" /></a></td>";
        }else{
            echo "<td> </td>";
        }
        if(canReadDoc($dat,$depart_id)){
            echo "<td align=\"center\"><a href=\"loadpdf.php?edoc_id=".$dat['edoc_id']."\" target=\"_blank\" ><img src=\"pic/pdfsmall.jpg\" /></a></td>";
        }else{
            echo "<td> </td>";
        }
		echo "<td>".nl2br(htmlspecialchars($dat['comment']))."</td>";
        if(canEditDoc($dat,$depart_id)){
            echo "<td align=\"center\"><img src=\"pic/drop.png\" /></td>";
        }else{
            echo "<td> </td>";
        }
		echo "</tr>";
	}
}

?>
